package com.itfreer.gis.map.resource;

import com.itfreer.gis.server.ProgressListener;
import com.itfreer.gis.server.storage.ServicePublishParas;
import com.itfreer.gis.server.tms.TMSServer;
import com.itfreer.gis.server.util.CommonUtil;
import com.itfreer.utils.ResponseEntity;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itfreer.form.api.BaseRestService;
import com.itfreer.form.api.BaseService;
import com.itfreer.gis.map.resource.entity.CustomServiceEntity;

import io.swagger.annotations.Api;

import java.util.*;

/**
 * 定义我的服务Rest服务接口
 */
@RestController
@RequestMapping("/giscustomservice")
@Api("我的服务")
public class RestCustomServiceService extends BaseRestService<CustomServiceEntity> {

	@Autowired
	private TMSServer tmsServer;

	@Autowired(required = false)
	private CustomServiceService service;

	@Reference(lazy = true, check = false)
	private CustomServiceService dubboService;

	@Override
	protected BaseService<CustomServiceEntity> getService() {
		if (service == null) {
			return dubboService;
		} else {
			return service;
		}
	}

	/**
	 * tms服务发布
	 */
	@ApiOperation(value = "tms服务发布", notes = "根据服务名称，数据源，切片范围，最大最小级别")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "id", value = "服务id", paramType = "path", dataType = "String", required = true),
			@ApiImplicitParam(name = "paras", value = "服务发布参数", paramType = "path", dataType = "Object", required = true) })
	@PostMapping(value = "publishtms/{id:.+}", produces = "application/json")
	public ResponseEntity<String> publishTms(@PathVariable("id") String id,
			@RequestBody List<ServicePublishParas> paras) throws Exception {
		if (paras.size() == 0) {
			return ResponseEntity.fail();
		}

		Date start = new Date();
		RestCustomServiceService that = this;

		String serviceName = paras.get(0).getServiceName();
		double[] extent = paras.get(0).getExtent();

		Map<Integer, String> sources = new HashMap<>();
		for (ServicePublishParas para : paras) {
			if (!sources.containsKey(para.getMinLevel())) {
				sources.put(para.getMinLevel(), para.getSource());
			}
		}

		CustomServiceEntity customServiceEntity = this.getEntity(id).getData();

		if (tmsServer == null){
			return ResponseEntity.fail();
		}

		tmsServer.generate(serviceName, sources, extent, new ProgressListener() {
			@Override
			public void onSize(int z, int count, int size, String msg) {
				if (size % 100 == 0 || size == count) {
					customServiceEntity.setCurSchedule(size);
					that.update(customServiceEntity);
				}
			}

			@Override
			public void onChange(int z, int count, String msg) {
				customServiceEntity.setAllSchedule(count);
				customServiceEntity.setCurSchedule(0);
				customServiceEntity.setCurLevel(z);
				that.update(customServiceEntity);
			}

			@Override
			public void onFail(String msg) {

			}

			@Override
			public void onSuccess() {
				customServiceEntity.setCurLevel(-1);
				customServiceEntity.setSpend(CommonUtil.getTimeSpan(new Date(), start));
				that.update(customServiceEntity);
			}
		});

		return new ResponseEntity<>(customServiceEntity.getSpend());
	}

}
